Charting top 5 counties where Youngkin outperformed Trump:
Youngkin outperformed Trump in every Virginia county in the 2020 election. Below is the process I did for charting the top 5 counties where Youngkin performed better than Trump, meaning the places where the difference between their percentages of the vote was greatest.
ggplot(toptencounties_YO, aes(x = locality, y = youngkin_overperform)) +geom_col(color ="#B32B2B", fill ="#B32B2B") +coord_flip() +scale_y_continuous(name ="Percent Overperform", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="Where Youngkin Outperformed Trump (%)", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()
Mapping Youngkin’s Margin of Victory over Trump
Below is a map showing the difference in the percentage of votes Youngkin received in Virginia counties and those that Trump received. The map uses shading to show the degree to which Youngkin outperformed Trump in every county.
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
To use now, restart R or run `readRenviron("~/.Renviron")`
Simple feature collection with 133 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE medincomeM
1 51105 Lee County, Virginia 22482 NA 37574 4430
2 51163 Rockbridge County, Virginia 22663 NA 57828 3580
3 51001 Accomack County, Virginia 33388 NA 50601 5803
4 51099 King George County, Virginia 26597 NA 101599 7080
5 51185 Tazewell County, Virginia 40615 NA 42937 3086
6 51029 Buckingham County, Virginia 16832 NA 49841 5835
7 51047 Culpeper County, Virginia 52021 NA 85274 5034
8 51179 Stafford County, Virginia 154204 NA 119818 3302
9 51085 Hanover County, Virginia 109232 NA 96911 2936
10 51009 Amherst County, Virginia 31381 NA 60876 3561
medageE medageM geometry
1 45.6 0.7 MULTIPOLYGON (((-83.67461 3...
2 49.2 0.6 MULTIPOLYGON (((-79.68695 3...
3 47.1 0.6 MULTIPOLYGON (((-75.74241 3...
4 38.3 0.4 MULTIPOLYGON (((-77.34744 3...
5 45.1 0.3 MULTIPOLYGON (((-81.90089 3...
6 43.0 0.8 MULTIPOLYGON (((-78.83284 3...
7 39.0 0.6 MULTIPOLYGON (((-78.22926 3...
8 35.9 0.2 MULTIPOLYGON (((-77.63422 3...
9 42.1 0.4 MULTIPOLYGON (((-77.79681 3...
10 44.9 0.5 MULTIPOLYGON (((-79.43695 3...
Warning: Found less unique colors (9) than unique zcol values (120)!
Interpolating color vector to match number of zcol values.
Charting top 5 counties where Biden outperformed McAuliffe:
McAuliffe did not outperform Biden in any county, as referenced in the data. For this reason, I decided to chart the top ten counties where Biden outperformed McAuliffe.
ggplot(toptencounties_BO, aes(x = locality, y = biden_overperform)) +geom_col(color ="#287AC7", fill ="#287AC7") +coord_flip() +scale_y_continuous(name ="Percent Overperform", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="Where Biden Outperformed McAuliffe (%)", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()
Mapping Biden’s Margin of Victory over McAuliffe
I mapped the difference in the percentage of votes Biden received compared to those McAuliffe received. Biden outperformed McAuliffe in every Virginia county. The darker the shading, the greater the victory.
Warning: Found less unique colors (9) than unique zcol values (115)!
Interpolating color vector to match number of zcol values.
Charting Where Youngkin Got the Highest Percentage of the Vote
Youngkin won the Virginia Governor race in 2020. Below is the code I used to create a chart showing the five counties in Virginia where Youngkin got the highest percentage of the vote.
ggplot(youngkin_top5, aes(x = locality, y = pct_youngkin)) +geom_col(color ="#FF3636", fill ="#FF3636") +coord_flip() +scale_y_continuous(name ="Percent Youngkin", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="Youngkin Percent of Votes", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()
Charting Where McAuliffe Got the Highest Percentage of the Vote
While McAuliffe did not won the Virginia Governor race, he did receive the highest percentage of the vote in some counties. I created a chart showing the top five counties in Virginia where McAuliffe got the highest percentage of the vote.
ggplot(mcauliffe_top5, aes(x = locality, y = pct_mcauliffe)) +geom_col(color ="#4CA0FF", fill ="#4CA0FF") +coord_flip() +scale_y_continuous(name ="Percent McAuliffe", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="McAuliffe Percent of Votes", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()
Source Code
---title: "Virginia Election Project"author: "Hannah Loder"execute: echo: trueformat: html: self-contained: true code-fold: true code-tools: true---```{r setup, include=FALSE, warning=FALSE, message=FALSE}knitr::opts_chunk$set(echo =TRUE)library(tidyverse)library(janitor)library(kableExtra)library(here)options(scipen =999)options(stringsAsFactors =FALSE)library(readxl) library(scales)library(lubridate)library(ggthemes)library(esquisse)library(tidyverse)library(tigris)library(sf)library(tidycensus)library(htmltools)library(janitor)library(here)library(mapview)library(leafsync)library(leaflet.extras2)options(tigris_class ="sf")joined_vacomparison <-readRDS(here("joined_vacomparison.rds"))``````{r}# cleaning up percentages joined_vacomparison <- joined_vacomparison %>%mutate(youngkin_overperform =round_half_up(youngkin_overperform, 2),mcauliffe_underperform =round_half_up(mcauliffe_overperform, 2), biden_overperform =round_half_up(biden_overperform, 2))```## Charting top 5 counties where Youngkin outperformed Trump:Youngkin outperformed Trump in every Virginia county in the 2020 election. Below is the process I did for charting the top 5 counties where Youngkin performed better than Trump, meaning the places where the difference between their percentages of the vote was greatest.```{r}toptencounties_YO <- joined_vacomparison %>%arrange(desc(youngkin_overperform)) %>%head(5)``````{r}ggplot(toptencounties_YO, aes(x = locality, y = youngkin_overperform)) +geom_col(color ="#B32B2B", fill ="#B32B2B") +coord_flip() +scale_y_continuous(name ="Percent Overperform", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="Where Youngkin Outperformed Trump (%)", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()```## Mapping Youngkin's Margin of Victory over TrumpBelow is a map showing the difference in the percentage of votes Youngkin received in Virginia counties and those that Trump received. The map uses shading to show the degree to which Youngkin outperformed Trump in every county.```{r}census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE, overwrite=TRUE) ``````{r}myvars <-c(totalpop ="B01003_001",medincome ="B19013_001",medage ="B01002_001")``````{r}va_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),state ="VA",output ="wide",geometry =TRUE)va_counties_withgeo``````{r}va_counties_withgeo <- va_counties_withgeo %>%mutate(NAME =str_replace(NAME, ", Virginia", ""),NAME =str_to_upper(NAME) )``````{r}joined <-left_join(va_counties_withgeo, joined_vacomparison, by =c("NAME"="locality"))``````{r}mylabel <- glue::glue("{joined$NAME} {joined$youngkin_overperform}")mypopup <- glue::glue("<strong>{joined$NAME}</strong><br /> Percent: {joined$youngkin_overperform}<br /> Total Population: {joined$totalpopE}" ) %>%lapply(htmltools::HTML)mapview(joined, zcol ="youngkin_overperform", col.regions = RColorBrewer::brewer.pal(9, "Reds"), alpha.regions =1,popup = mypopup, label = mylabel)```## Charting top 5 counties where Biden outperformed McAuliffe:McAuliffe did not outperform Biden in any county, as referenced in the data. For this reason, I decided to chart the top ten counties where Biden outperformed McAuliffe.```{r}toptencounties_BO <- joined_vacomparison %>%arrange(desc(biden_overperform)) %>%head(5)``````{r}ggplot(toptencounties_BO, aes(x = locality, y = biden_overperform)) +geom_col(color ="#287AC7", fill ="#287AC7") +coord_flip() +scale_y_continuous(name ="Percent Overperform", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="Where Biden Outperformed McAuliffe (%)", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()```## Mapping Biden's Margin of Victory over McAuliffeI mapped the difference in the percentage of votes Biden received compared to those McAuliffe received. Biden outperformed McAuliffe in every Virginia county. The darker the shading, the greater the victory.```{r}map2label <- glue::glue("{joined$NAME} {joined$biden_overperform}")popupmap2 <- glue::glue("<strong>{joined$NAME}</strong><br /> Percent: {joined$biden_overperform}<br /> Total Population: {joined$totalpopE}" ) %>%lapply(htmltools::HTML)mapview(joined, zcol ="biden_overperform", col.regions = RColorBrewer::brewer.pal(9, "Blues"), alpha.regions =1,label = map2label, popup = popupmap2)```## Charting Where Youngkin Got the Highest Percentage of the VoteYoungkin won the Virginia Governor race in 2020. Below is the code I used to create a chart showing the five counties in Virginia where Youngkin got the highest percentage of the vote.```{r}youngkin_top5 <- joined_vacomparison %>%arrange(desc(pct_youngkin)) %>%head(5)``````{r}ggplot(youngkin_top5, aes(x = locality, y = pct_youngkin)) +geom_col(color ="#FF3636", fill ="#FF3636") +coord_flip() +scale_y_continuous(name ="Percent Youngkin", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="Youngkin Percent of Votes", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()```## Charting Where McAuliffe Got the Highest Percentage of the VoteWhile McAuliffe did not won the Virginia Governor race, he did receive the highest percentage of the vote in some counties. I created a chart showing the top five counties in Virginia where McAuliffe got the highest percentage of the vote.```{r}mcauliffe_top5 <- joined_vacomparison %>%arrange(desc(pct_mcauliffe)) %>%head(5)``````{r}ggplot(mcauliffe_top5, aes(x = locality, y = pct_mcauliffe)) +geom_col(color ="#4CA0FF", fill ="#4CA0FF") +coord_flip() +scale_y_continuous(name ="Percent McAuliffe", labels = scales::comma) +scale_x_discrete(name ="County") +labs(title ="McAuliffe Percent of Votes", subtitle ="Top 5 VA Counties 2020",caption ="VA Department of Elections") +theme_fivethirtyeight()```